const { createWebAPIRequest } = require('../../util/util')

module.exports = [
  {
    /**
    * @api {get} /music/user/playlist [获取用户歌单]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid  用户id
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量(貌似无效)
    */
    url: '/music/user/playlist',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        uid: req.query.uid,
        limit: req.query.limit || 20, // 貌似无效
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/user/playlist',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
          // console.log(JSON.parse(musicReq))
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/user/audio [获取用户电台]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid  用户id
    */
    url: '/music/user/audio',
    request: (req, res) => {
      const data = {
        userId: req.query.uid,
        csrf_token: ''
      }
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''

      createWebAPIRequest(
        'music.163.com',
        '/weapi/djradio/get/byuser',
        'POST',
        data,
        cookie,
        musicReq => {
          res.setHeader('Content-Type', 'application/json')
          res.send(musicReq)
        },
        err => {
          res.status(500).send({err})
        }
      )
    }
  }, {
    /**
    * @api {get} /music/user/cloud [云盘数据]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid  用户id
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量(貌似无效)
    */
    url: '/music/user/cloud',
    request: (req, res) => {
      const data = {
        limit: req.query.limit || 20,
        offset: req.query.offset || 0,
        csrf_token: ''
      }
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      createWebAPIRequest(
        'music.163.com',
        '/weapi/v1/cloud/get',
        'POST',
        data,
        cookie,
        musicReq => {
          res.setHeader('Content-Type', 'application/json')
          res.send(musicReq)
        },
        err => {
          res.status(500).send({err})
        }
      )
    }
  }, {
    /**
    * @api {get} /music/user/cloud/search [云盘数据详情? 暂时不要使用]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} id
    */
    url: '/music/user/cloud/search',
    request: (req, res) => {
      const data = {
        byids: req.query.id,
        id: req.query.id,
        csrf_token: ''
      }
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      createWebAPIRequest(
        'music.163.com',
        '/weapi/v1/cloud/get/byids',
        'POST',
        data,
        cookie,
        musicReq => {
          res.setHeader('Content-Type', 'application/json')
          res.send(musicReq)
        },
        err => {
          res.status(500).send({err})
        }
      )
    }
  }, {
    /**
    * @api {get} /music/user/event [用户动态]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid
    */
    url: '/music/user/event',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const id = req.query.uid
      const data = {
        time: -1,
        getcounts: true,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/event/get/${id}`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/user/detail [用户详情]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid
    */
    url: '/music/user/detail',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const id = req.query.uid
      const data = {
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/v1/user/detail/${id}`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/user/dj [用户电台]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/user/dj',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const id = req.query.uid
      const data = {
        offset: req.query.offset || 0,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/dj/program/${id}`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/user/followeds [获取用户粉丝列表]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid uid=32953014
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/user/followeds',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        userId: req.query.uid,
        offset: req.query.offset || 0,
        limit: req.query.limit || 30,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/user/getfolloweds/`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/user/follows [获取用户关注列表]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} uid uid=32953014
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/user/follows',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const id = req.query.uid
      const data = {
        offset: req.query.offset || 0,
        limit: req.query.limit || 20,
        order: true
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/user/getfollows/${id}`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {post} /music/user/subcount [获取用户信息 , 歌单，收藏，mv, dj 数量]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 登陆后调用此接口 , 可以获取用户信息
    * @apiParam {Number} uid uid=32953014
    */
    url: '/music/user/subcount',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/subcount',
        'POST',
        data,
        cookie,
        musicReq => res.send(musicReq),
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {post} '/music/user/record [获取用户播放记录]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 登陆后调用此接口 , 传入用户 id, 可获取用户播放记录
    * @apiParam {Number} uid uid=32953014
    * @apiParam {Number} type type=1时只返回weekData, type=0时返回allData
    */
    url: '/music/user/record',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      // type=1时只返回weekData, type=0时返回allData
      const data = {
        type: req.query.type || 0,
        uid: req.query.uid, // 用户 id,
        csrf_token: ''
      }
      const action = `/weapi/v1/play/record`
      createWebAPIRequest(
        'music.163.com',
        action,
        'POST',
        data,
        cookie,
        musicReq => res.send(musicReq),
        err => res.status(500).send({err})
      )
    }
  }
]
